﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BiocityFilmquizAdmin.Src;
using BiocityFilmquizAdmin.Src.DatabaseAccess;
using BiocityFilmquizAdmin.Src.Domain;

namespace BiocityFilmquizAdmin
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void Page_PreRender(object sender, System.EventArgs e)
        {
            renderLoggedInBoxes();
        }

        private void renderLoggedInBoxes()
        {
            if (Session["loggedIn"] != null && (bool)Session["loggedIn"])
            {
                //If logged in
                loggedInDiv.Visible = true;
                notLoggedInDiv.Visible = false;

                populateRoundDropDown();

                int currentRound = Statics.getInstance().getCurrentRound();

                activeRoundLabel.Text = "Runde " + currentRound + " er aktiv i øjeblikket";
            }
            else
            {
                //If not logged in
                loggedInDiv.Visible = false;
                notLoggedInDiv.Visible = true;
            }
        }

        private void populateRoundDropDown()
        {
            List<Round> rounds = AdminHelper.getInstance().getAllRounds();

            if (rounds.Count() != roundDropDown.Items.Count)
            {
                roundDropDown.Items.Clear();
                foreach (var round in rounds)
                {
                    roundDropDown.Items.Add(round.roundId.ToString());
                }

            }
            int currentRound = Statics.getInstance().getCurrentRound();

            roundDropDown.SelectedIndex = currentRound - 1;
        }

        protected void activateRoundButton_Click(object sender, EventArgs e)
        {
            int selectedRound = Int32.Parse(roundDropDown.SelectedValue);
            AdminHelper.getInstance().setProperty("currentRound", selectedRound.ToString());

            Statics.getInstance().getCurrentRoundFromDataBase();
        }
    }
}
